import React, { useEffect, useRef, useState } from "react";
import SongList from "./SongList";
import { Dimensions, Image, ScrollView, StatusBar, TouchableOpacity, View } from "react-native";
import Colors from "../../util/Colors";
import { FAB } from "@rneui/base";
import AntDesign from "react-native-vector-icons/AntDesign";
import { get163AlbumDetail, get163PlayListDetail, get163SongsBySinger } from "../../request/neteaseRequest";
import { getQAlbumDetail, getQPlayListDetail, getQSongsBySinger } from "../../request/QQRequest";
import Toast from "react-native-easy-toast";
import { getMgAlbumBySinger, getMgAlbumDetail, getMgPlayListDetail } from "../../request/miguRequest";
import { getKwAlbumDetail, getKwPlayListDetail, getKwSongsBySinger } from "../../request/kuwoRequest";
import _ from 'lodash';
import { getKgPlayListDetail } from "../../request/kugouRequest";

export default function(props) {
  const toast = useRef(null);




  const [more,setMore] = useState(false);
  const [page,setPage] = useState(1);
  const [perPage,setPerPage] = useState(30);
  const [item,setItem] = useState(props.route.params.item);
  const [isAlbum,setisAlbum] = useState(props.route.params.item.isAlbum || false);
  const [isSinger,setisSinger] = useState(props.route.params.item.isSinger || false);
  const [lists,setLists] = useState({img:item.img?.uri || item.img || ''});
  useEffect(() => {
    //console.log(item);
    init();
  },[])
  async function init(){
    if(isSinger){
      //console.log(1);
      switch (item.from) {
        case '163':
        case 163:
          //console.log(2);
          let offset = perPage * (page - 1);
          await get163SongsBySinger(item.id, offset,perPage,(r) => {
            //console.log(r);

            setLists({
              list:r.v,
              img:item.img.uri || item.img
            })
            if(r.count > page * perPage){
              setMore(true);
            }else {
              setMore(false);
            }
          }).catch(() => {
            toast.current.show('可能需要登录或者VIP');
          })
          break;
        case 'qq':
          await getQSongsBySinger(item.id, page,perPage,(r) => {
            //console.log(r);
            setLists({
              list:r.v,
              img:item.img.uri || item.img
            })
            if(r.count > page * perPage){
              setMore(true);
            }else {
              setMore(false);
            }
          }).catch((r) => {
            //console.log('error:::' + r);
            toast.current.show('可能需要登录或者VIP');
          })
          break;
        case 'kuwo':
          await getKwSongsBySinger(item.id, page,perPage,(r) => {
            //console.log(r);
            setLists({
              list:r.v,
              img:item.img.uri || item.img
            })
            if(r.count > page * perPage){
              setMore(true);
            }else {
              setMore(false);
            }
          }).catch((r) => {
            //console.log('error:::' + r);
            toast.current.show('可能需要登录或者VIP');
          })
          break;
        case 'migu':
          await getMgAlbumBySinger(item.id, page,perPage,(r) => {
            //console.log(r);
            setLists({
              list:r.v,
              img:item.img.uri || item.img
            })
            if(r.count > page){
              setMore(true);
            }else {
              setMore(false);
            }
          }).catch((r) => {
            //console.log('error:::' + r);
            toast.current.show('可能需要登录或者VIP');
          })
          break;
      }
    }else
      if(isAlbum){
      //console.log('isalbum');
      switch (item.from) {
        case '163':
        case 163:
          await get163AlbumDetail(item.id, (r) => {
            //console.log(r);

            setLists({
              list:r,
              img:item.img.uri || item.img
            })
          }).catch(() => {
            toast.current.show('可能需要登录或者VIP');
          })
          break;
        case 'qq':
          await getQAlbumDetail(item.id, (r) => {
            //console.log(r);
            setLists({
              list:r,
              img:item.img.uri || item.img
            })
          }).catch((r) => {
            //console.log('error:::' + r);
            toast.current.show('可能需要登录或者VIP');
          })
          break;
        case 'migu':
          await getMgAlbumDetail(item.id, (r) => {
            //console.log(r);
            setLists({
              list:r,
              img:item.img.uri || item.img
            })
          }).catch((r) => {
            //console.log('error:::' + r);
            toast.current.show('可能需要登录或者VIP');
          })
          break;
        case 'kuwo':
          await getKwAlbumDetail(item.id, (r) => {
            //console.log(r);
            setLists({
              list:r,
              img:item.img.uri || item.img
            })
          }).catch((r) => {
            //console.log('error:::' + r);
            toast.current.show('可能需要登录或者VIP');
          })
          break;
      }
    }else
    {
      switch (item.from) {
        case '163':
        case 163:
          await get163PlayListDetail(item.id, (r) => {
            //console.log(r);
            setLists(r)
          }).catch(() => {
            toast.current.show('可能需要登录或者VIP');
          })
          break;
        case 'qq':
          await getQPlayListDetail(item.id, (r) => {
            //console.log(r);
            setLists(r)
          }).catch((r) => {
            //console.log('error:::' + r);
            toast.current.show('可能需要登录或者VIP');
          })
          break;
        case 'migu':
          await getMgPlayListDetail(item.id, (r) => {
            //console.log(r);
            setLists(r)
          }).catch((r) => {
            //console.log('error:::' + r);
            toast.current.show('可能需要登录或者VIP');
          })
          break;
        case 'kuwo':
          await getKwPlayListDetail(item.id, (r) => {
            //console.log(r);
            setLists(r)
          }).catch((r) => {
            //console.log('error:::' + r);
            toast.current.show('可能需要登录或者VIP');
          })
          break;
        case 'kugou':
          await getKgPlayListDetail(item.id, (r) => {
            //console.log(r);
            setLists(r)
          }).catch((r) => {
            //console.log('error:::' + r);
            toast.current.show('可能需要登录或者VIP');
          })
          break;
      }

    }

  }

  async function morePress(){
    let newPage = page + 1;
    let newList = {};
    //console.log(item);
    switch (item.from) {
      case '163':
      case 163:
        let offset = perPage * (newPage - 1);
        await get163SongsBySinger(item.id, offset,perPage,(r) => {
          //console.log(r);
          newList.list = _.concat(lists.list,r.v)
          newList.img = item.img.uri || item.img;
       setLists(newList);
          if(r.count > newPage * perPage){
            setMore(true);
          }else {
            setMore(false);
          }
          setPage(newPage);
        }).catch(() => {
          toast.current.show('可能需要登录或者VIP');
        })
        break;
      case 'qq':
        await getQSongsBySinger(item.id, newPage,perPage,(r) => {
          //console.log(r);
          newList.list = _.concat(lists.list,r.v)
          newList.img = item.img.uri || item.img;

          //console.log(newList);
          setLists(newList);
          if(r.count > newPage * perPage){
            setMore(true);
          }else {
            setMore(false);
          }
          setPage(newPage);
        }).catch((r) => {
          //console.log('error:::' + r);
          toast.current.show('可能需要登录或者VIP');
        })
        break;
      case 'kuwo':
        await getKwSongsBySinger(item.id, newPage,perPage,(r) => {
          //console.log(r);
          newList.list = _.concat(lists.list,r.v)
          newList.img = item.img.uri || item.img;
          setLists(newList);
          if(r.count > newPage * perPage){
            setMore(true);
          }else {
            setMore(false);
          }
          setPage(newPage);
        }).catch((r) => {
          //console.log('error:::' + r);
          toast.current.show('可能需要登录或者VIP');
        })
        break;
      case 'migu':
        await getMgAlbumBySinger(item.id, newPage,perPage,(r) => {
          //console.log(r);
          newList.list = _.concat(lists.list,r.v)
          newList.img = item.img.uri || item.img;
          setLists(newList);
          if(r.count > newPage){
            setMore(true);
          }else {
            setMore(false);
          }
          setPage(newPage);
        }).catch((r) => {
          //console.log('error:::' + r);
          toast.current.show('可能需要登录或者VIP');
        })
        break;
    }
  }


  function pressListBtn(){
    props.setToAddValue(lists.list);
    props.setToAdd(true);
  }
  function add(v){
    props.setToAddValue(v);
    props.setToAdd(true);
  }
  return (
    <View style={{flex:1,marginBottom:55}}>
      <StatusBar
        barStyle={'light-content'}
        backgroundColor={Colors.colorPrimary}
        translucent={false}
      />
      <Toast
        ref={toast}
        position="center"
        style={{ backgroundColor: Colors.colorPrimary }}
      />
      <ScrollView>
        <Image source={{uri:lists.img || ''}}
        style={{width:width,height:height/3}}
        >
        </Image>
        <FAB
          color={Colors.colorLight}
          visible={true}
          icon={<AntDesign name={'pluscircleo'} color={'white'} style={{marginBottom:-4}} size={27}/>}
          size="large"
          style={{ position:'absolute',right:10,top:height/3 - 40 }}
          onPress={pressListBtn}
        />
        <View style={{backgroundColor:'rgba(255,255,255,0.11)',width:width,position:'absolute'}}>

          <TouchableOpacity onPress={()=>props.navigation.pop()}>
            <AntDesign name={'left'} style={{marginLeft:10}} size={35} color={'white'}/>
          </TouchableOpacity>
        </View>

        <SongList add={props.addPlayList} addToList={add} more={more} onMore={morePress} play={props.setCurrentMusic}  navigation={props.navigation} songs={lists.list} isAlbum={isAlbum}/>
      </ScrollView>
    </View>
  );
}
const {height, width} = Dimensions.get('window');
